package com.adspert.dmo.util;

import java.util.Calendar;
import java.util.Date;

import com.adspert.commons.Constants;
import com.adspert.commons.util.DateUtils;
import com.adspert.commons.vo.DateRange;

/**
 * @author Jensen
 * @version 创建时间：2009-1-8 下午03:50:17
 *          
 */

public class TimeRangeUtil {
	/**
	 * Jensen 2009-2-20 下午03:24:17
	 *计算最后一段时间的范围（一星期或一个月），如果活动的结束时间大于当前时间，就按活动的结束时间倒推一个范围，否则
     *按照当前时间倒推一个范围。
	 */
	public static DateRange timeRange(Integer weekOrMonth, Date endDate) {
		int range = 0;
		Date startTime;
		Date endTime;
		if (weekOrMonth.equals(Constants.DMO_MEASURE_BYWEEK)) {
			range = 7;
		}
		if (weekOrMonth.equals(Constants.DMO_MEASURE_BYMONTH)) {
			range = 30;
		}
		Calendar c = Calendar.getInstance();
		if (c.after(endDate)) {
			Calendar c2 = Calendar.getInstance();
			c2.setTime(endDate);
			c2.add(Calendar.DATE, -range);
			c2.set(Calendar.HOUR_OF_DAY, 0);
			c2.set(Calendar.MINUTE, 0);
			c2.set(Calendar.SECOND, 0);
			startTime = c.getTime();
			c2.add(Calendar.DATE, +range - 1);
			c2.set(Calendar.HOUR_OF_DAY, 23);
			c2.set(Calendar.MINUTE, 59);
			c2.set(Calendar.SECOND, 59);
			endTime = c.getTime();
		} else {
			c.add(Calendar.DATE, -range);
			c.set(Calendar.HOUR_OF_DAY, 0);
			c.set(Calendar.MINUTE, 0);
			c.set(Calendar.SECOND, 0);
			startTime = c.getTime();
			c.add(Calendar.DATE, +range - 1);
			c.set(Calendar.HOUR_OF_DAY, 23);
			c.set(Calendar.MINUTE, 59);
			c.set(Calendar.SECOND, 59);
			endTime = c.getTime();
		}
		DateRange dr = new DateRange();
		dr.setStartDate(startTime);
		dr.setEndDate(endTime);
		return dr;
	}

	/**
	 * 
	 * Jensen 2009-1-16 下午05:52:10 根据一个dateRange返回一个显示方式 如果dateRange的时间差小于21
	 * 返回按天显示， 如果大于21小于63，返回按星期显示，如果大于63返回按月显示
	 * 
	 */
	public static int findPeriodType(DateRange dateRange) {
		int showType = 0;
		Date startDate = dateRange.getStartDate();
		Date endDate = dateRange.getEndDate();
		int interval = DateUtils.getDaysBetween(startDate, endDate);
		if (interval <= 63) {
			showType = Constants.PERIOD_DAILY;
		} else if (interval <= 63) {
			showType = Constants.PERIOD_WEEKLY;
		} else {
			showType = Constants.PERIOD_MONTHLY;
		}
		return showType;
	}
	/**
	 * 返回参数时间后的一个星期（当天除外）
	 * 如果输入的参数与今天间隔不到一个星期，则返回参数时间和今天的时间
	 * Jensen 2009-2-17 下午03:33:25
	 *
	 */
	public static DateRange findBenchmarkDateRange(Date date){
		int flag=DateUtils.getDaysBetween(date,new Date());	
		DateRange dr = new DateRange();
		if(flag>=7){
		Calendar c=Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE,1);
		dr.setStartDate(c.getTime());
		c.add(Calendar.DATE,6);
		dr.setEndDate(c.getTime());
		}else{
			dr.setStartDate(date);
			dr.setEndDate(new Date());			
		}
		return dr;
	}
	/**
	 * 返回上一个完整数据周（从星期一开始）
	 * Jensen 2009-2-18 下午03:51:31
	 */
	public static DateRange findLatestWeekDateRange(Date startDate,Date endDate){
		DateRange dr=new DateRange();
		Calendar cs=Calendar.getInstance();
		cs.setTime(startDate);
		//如果开始时间是星期一，则从下个星期一开始统计，否则从下下个星期一开始统计
		if(cs.get(Calendar.DAY_OF_WEEK)==Calendar.MONDAY){
			cs.add(Calendar.DATE,7);
		}else{
			if(cs.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
				cs.add(Calendar.DATE,8);
			}else{
				cs.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
				cs.add(Calendar.DATE,14);
			}
		}		
		if(cs.getTime().after(new Date())){
			//不取任何数据
			Calendar calendar=Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DATE,1);
			dr.setStartDate(calendar.getTime());
			dr.setEndDate(new Date());
			return dr;
		}
		//如果结束时间不是星期天，则本周的数据不算，将结束时间设置为上个星期天即可
		Calendar ce=Calendar.getInstance();
		ce.setTime(endDate);
		if(ce.get(Calendar.DAY_OF_WEEK)!=Calendar.SUNDAY){
			ce.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
		}
		//如果今日时间大于改变后的结束时间，则始终返回改变后的那一周
		if((new Date()).after(ce.getTime())){
			dr.setEndDate(ce.getTime());
			ce.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
			dr.setStartDate(ce.getTime());
		}else{
		Calendar c=Calendar.getInstance();
		c.setTime(new Date());
		//今天是星期天，则取前13到前7天的数据，如果今天不是星期天，则结束时间为上个星期天
		if(!((c.get(Calendar.DAY_OF_WEEK))== Calendar.SUNDAY)){
		c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
		}else{
			c.add(Calendar.DATE,-7);
		}
		dr.setEndDate(c.getTime());
		c.add(Calendar.DATE,-6);
		dr.setStartDate(c.getTime());
		}
		return dr;
	}
    /**
     * 返回上一个完整数据月
     * Jensen 2009-2-18 下午04:39:23
     */
	public static DateRange findLatestMonthDateRange(Date startDate,Date endDate){
		DateRange dr=new DateRange();
	    Calendar c=Calendar.getInstance();
	    c.setTime(startDate);
	    if(c.get(Calendar.DAY_OF_MONTH)==1){
	    	c.add(Calendar.MONTH,1);
	    }else{
	    	c.add(Calendar.MONTH,2);
	    	c.set(Calendar.DAY_OF_MONTH,1);
	    }
	    if((new Date()).before(c.getTime())){
	    	//不取任何数据
	    	Calendar calendar=Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DATE,1);
			dr.setStartDate(calendar.getTime());
			dr.setEndDate(new Date());
			return dr;
	    }
	    Calendar c1=Calendar.getInstance();
	    //先判断结束日期是不是结束当月的最后一天
	    c1.setTime(endDate);
	    int date1=c1.get(Calendar.DAY_OF_MONTH);
	    c1.add(Calendar.MONTH,1);
	    c1.set(Calendar.DAY_OF_MONTH,1);
	    c1.add(Calendar.DATE,-1);
	    int date2=c1.get(Calendar.DAY_OF_MONTH);
	   //结束日期是当月的最后一天
	    if(date1==date2){
	      if((new Date()).after(c1.getTime())){
	    	 dr.setEndDate(c1.getTime());
	    	 c1.set(Calendar.DAY_OF_MONTH,1);
	    	 dr.setStartDate(c1.getTime());
	    	 return dr;
	      }
	    }else{
	    	//结束日期不是当月的最后一天
	    	if((new Date()).after(c1.getTime())){
	    		c1.set(Calendar.DAY_OF_MONTH,1);
	    		c1.add(Calendar.DATE,-1);
	    		dr.setEndDate(c1.getTime());
	    		c1.set(Calendar.DAY_OF_MONTH,1);
	    		dr.setStartDate(c1.getTime());	    		
	    	}    	
	    }
	    Calendar ct=Calendar.getInstance();
	    ct.setTime(new Date());
	    ct.set(Calendar.DAY_OF_MONTH,1);
	    ct.add(Calendar.DATE,-1);
	    dr.setEndDate(ct.getTime());
	    ct.set(Calendar.DAY_OF_MONTH,1);
	    dr.setStartDate(ct.getTime());
		return dr;
	}
	/**
	 * 求平均月时间，如果开始月小于5号，算一个月，否则不算，最后一个月(本月)不算月
	 * Jensen 2009-2-19 上午11:59:50
	 */
	public static DateRange findAverageMonthDateRange(Date startDate,Date endDate){
		DateRange dr=new DateRange();
		Date standard=new Date();;
		if(!endDate.after(new Date())){
			standard=endDate;
		}
		Calendar c=Calendar.getInstance();
		c.setTime(startDate);
		if(c.get(Calendar.DAY_OF_MONTH)>5){
			c.add(Calendar.MONTH,1);
			c.set(Calendar.DAY_OF_MONTH,1);
		}
		dr.setStartDate(c.getTime());
		c.setTime(standard);
		c.set(Calendar.DAY_OF_MONTH,1);
		c.add(Calendar.DATE,-1);
		dr.setEndDate(c.getTime());		
		return dr;
	}
	/**
	 * 返回当前时间和参数时间的月份差,参数时间小于5等于5号算一个月，本月不算
	 * Jensen 2009-2-19 下午12:32:44
	 */
	public static int findMonthsAmount(Date date,Date endDate){
		int amount=0;
		Calendar c=Calendar.getInstance();
		Date standard=new Date();;
		if(!endDate.after(new Date())){
			standard=endDate;
		}
		c.setTime(date);
		if(c.get(Calendar.DAY_OF_MONTH)>5){
			c.add(Calendar.MONTH,1);
		}
	    int year1=c.get(Calendar.YEAR);
	    int month1=c.get(Calendar.MONTH);
	    Calendar c2=Calendar.getInstance();
	    c2.setTime(standard);
	    int year2=c2.get(Calendar.YEAR);
	    int month2=c2.get(Calendar.MONTH);
	    amount=(year2-year1)*12+month2-month1;
		return amount;
	}
	/**
	 * 求平均周时间，开始一周不要，最后一周不要
	 * Jensen 2009-2-19 下午12:52:39
	 */
	public static DateRange findAverageWeekDateRange(Date startDate,Date endDate){
		DateRange dr=new DateRange();
		Date standard=new Date();;
		if(!endDate.after(new Date())){
			standard=endDate;
		}
		Calendar c=Calendar.getInstance();
		c.setTime(startDate);
		c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
		c.add(Calendar.DATE, 8);
		dr.setStartDate(c.getTime());
		Calendar c2=Calendar.getInstance();
		c2.setTime(standard);
		c2.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
		c2.add(Calendar.DATE,-1);
		dr.setEndDate(c2.getTime());
		return dr;
	}
	/**返回参数时间到现在(如果第二个参数早于现在时间，则取第二个参数时间)的星期数目，第一个星期和最后一个星期不算
	 * Jensen 2009-2-12 下午02:28:48
	 */
	public static int findWeeksAmount(Date startDate,Date endDate){
		int amount=0;
		DateRange dr=findAverageWeekDateRange(startDate,endDate);
		amount=(DateUtils.getDaysBetween(dr.getStartDate(),dr.getEndDate())+1)/7;		
		return amount;
	}
	/**
	 *返回当前时间的前一个星期的日期(以星期一开始)
	 * Jensen 2009-2-23 上午10:25:52
	 */
	public static DateRange findLastWeekDateRange(){
		return findWeekBeforeDate(new Date());
	}
	/**
	 * 返回当前时间的前一个月时间
	 * Jensen 2009-2-23 上午10:34:48
	 */
	public static DateRange findLastMonthDateRange(){
		DateRange dr=new DateRange();
	    Calendar c=Calendar.getInstance();
	    c.set(Calendar.DAY_OF_MONTH,1);
	    c.add(Calendar.DAY_OF_MONTH,-1);
	    dr.setEndDate(c.getTime());
	    c.set(Calendar.DAY_OF_MONTH,1);
	    dr.setStartDate(c.getTime());
		return dr;		
	}
	/**
	 * 返回参数时间是本周的第几天(星期一是第一天)
	 * Jensen 2009-2-24 上午09:56:14
	 */
	public static int findDaysInWeek(Date endDate){
		int days=0;
		Calendar c=Calendar.getInstance();
		c.setTime(endDate);
		if(c.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
			days=7;
		}else{
			days=c.get(Calendar.DAY_OF_WEEK)-1;
		}
		return days;
	} 
	/**
	 * 返回参数时间是本月的第几天
	 * Jensen 2009-2-24 上午10:12:44
	 *
	 */
	public static int findDaysInMonth(Date endDate){
		int days=0;
		Calendar c=Calendar.getInstance();
		c.setTime(endDate);
		days=c.get(Calendar.DAY_OF_MONTH);
		return days;
	}
	
	public static DateRange findWeekBeforeDate(Date date){
		DateRange dr=new DateRange();
		Calendar c=Calendar.getInstance();
		c.setTime(date);
		if(c.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
			c.add(Calendar.DATE,-7);
			dr.setEndDate(c.getTime());
			c.add(Calendar.DATE,-6);
			dr.setStartDate(c.getTime());
		}else{
			//如果不是星期天
			c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
			dr.setEndDate(c.getTime());
			c.add(Calendar.DATE,-6);
			dr.setStartDate(c.getTime());			
		}
		return dr;
	}
	
	
	public static void main(String[] args) {
       Calendar cs=Calendar.getInstance();
       cs.set(Calendar.YEAR,2009);
       cs.set(Calendar.MONTH,0);
       cs.set(Calendar.DAY_OF_MONTH,1);
		Calendar ce=Calendar.getInstance();
		ce.set(Calendar.MONTH,1);
		ce.set(Calendar.DAY_OF_MONTH,28);
	   DateRange dr=new DateRange();
//	   dr=findLastWeekDateRange();
	   dr=findLatestMonthDateRange(cs.getTime(),ce.getTime());
	   System.out.println(dr.getStartDate());
	   System.out.println(dr.getEndDate());
	   System.out.println(findMonthsAmount(cs.getTime(),ce.getTime()));
	   System.out.println(findWeeksAmount(cs.getTime(),ce.getTime()));
	   System.out.println("end...");
	}
}
